package com.mm.controller.system;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mm.entity.MmRole;
import com.mm.exception.BusinessException;
import com.mm.service.impl.MmRoleServiceImpl;
import com.mm.enums.ResultCodeEnum;
import com.mm.utils.ApiResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author wdx
 * @since 2020-09-16
 */
@Api(tags = "角色API")
@Validated
@RestController
@RequestMapping("/mm-role")
public class MmRoleController {

    @Autowired
    MmRoleServiceImpl mmRoleService;

    @ApiOperation(value = "分页列表")
    @GetMapping("/{pageNo}/{pageSize}")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNo", value = "当前页", defaultValue = "1", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "一页条数", defaultValue = "10", dataType = "int", paramType = "query")
    })
    public ApiResult<MmRole> page(@NotNull(message = "当前页不能为空") @PathVariable("pageNo") Integer pageNo, @NotNull(message = "页条数不能为空") @PathVariable("pageSize") Integer pageSize) {
        IPage<MmRole> page = mmRoleService.page(new Page<>(pageNo, pageSize));
        return ApiResult.success(page);
    }

    @ApiOperation(value = "保存")
    @PostMapping
    public ApiResult save(@Valid @RequestBody MmRole mRole) {
        boolean save = mmRoleService.save(mRole);
        if (!save) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "详情")
    @GetMapping("/{id}")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    public ApiResult<MmRole> detail(@NotNull(message = "ID不能为空") @PathVariable Integer id) {
        MmRole mRole = mmRoleService.getById(id);
        if (mRole == null) {
            throw new BusinessException(ResultCodeEnum.NO_DATA);
        }
        return ApiResult.success(mRole);
    }


    @ApiOperation(value = "修改")
    @PutMapping
    public ApiResult update(@RequestBody MmRole mRole) {
        boolean update = mmRoleService.updateById(mRole);
        if (!update) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParam(name = "id", value = "主键ID", dataType = "int", paramType = "query")
    @DeleteMapping("/{id}")
    public ApiResult del(@NotNull(message = "ID不能为空") @PathVariable Integer id) {
        boolean del = mmRoleService.removeById(id);
        if (!del) {
            throw new BusinessException();
        }
        return ApiResult.success();
    }

}
